// Copyright 2009 StarCite Inc.  All rights reserved.
// HornetQTool.java 3:34:26 PM danny.liu
package com.billowsoft.test.queue.hornetq;

import java.util.List;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.management.MBeanServerConnection;
import javax.management.MBeanServerInvocationHandler;
import javax.naming.InitialContext;

import org.hornetq.api.core.management.MessageCounterInfo;
import org.hornetq.api.jms.management.JMSQueueControl;

/**
 * @author danny.liu Jun 1, 2012
 * @version 1.0
 * 
 */
public class HornetQTool {

	public static void showStatus() {
		MBeanServerConnection mbsc = null;
		JMSQueueControl queueControl = (JMSQueueControl) MBeanServerInvocationHandler
				.newProxyInstance(mbsc, on, JMSQueueControl.class, false);
		// message counters are retrieved as a JSON String
		String counters = queueControl.listMessageCounter();
		// use the MessageCounterInfo helper class to manipulate message
		// counters more easily
		MessageCounterInfo messageCounter = MessageCounterInfo.fromJSON(counters);
		System.out.format(
				"%s message(s) in the queue (since last sample: %s)\n",
				messageCounter.getDepth(), messageCounter.getDepthDelta());
	}

	public static void sendMsg(List<String> messages) throws Exception {
		InitialContext ic = new InitialContext();
		ConnectionFactory cf = (ConnectionFactory) ic
				.lookup("/ConnectionFactory");
		Queue orderQueue = (Queue) ic.lookup("/queues/TestQueue");
		Connection connection = cf.createConnection();
		Session session = connection.createSession(false,
				Session.AUTO_ACKNOWLEDGE);
		MessageProducer producer = session.createProducer(orderQueue);
		for (String msg : messages) {
			TextMessage message = session.createTextMessage(msg);
			producer.send(message);
		}
	}
}
